package data_structure;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class leetcode_0239 {
    public int[] maxSlidingWindow(int[] nums, int k) {
        int len=nums.length;
        int len2=len-k+1;
        int[] result=new int[len2];
        int current=0;
        LinkedList<Integer> queue=new LinkedList<>();
        for(int i=0;i<k;++i){
            while(!queue.isEmpty()&&queue.getLast()<nums[i]){
                queue.removeLast();
            }
            queue.add(nums[i]);
        }
        result[current++]= queue.getFirst();
        for(int i=1;i+k-1<len;++i){
            if(!queue.isEmpty()&&queue.getFirst()==nums[i-1]){
                queue.removeFirst();
            }
            while(!queue.isEmpty()&&queue.getLast()<nums[i+k-1]){
                queue.removeLast();
            }
            queue.add(nums[i+k-1]);
            result[current++]=queue.getFirst();
        }
        return result;
    }
}
